package cn.group01.git.controller;

import cn.group01.git.annotations.LogOperation;
import cn.group01.git.common.CodeMsg;
import cn.group01.git.common.Operation;
import cn.group01.git.common.ResponseResult;
import cn.group01.git.dto.UserPageDTO;
import cn.group01.git.entity.User;
import cn.group01.git.form.LoginForm;
import cn.group01.git.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
/**
 * @description:
 * @author: 吴凡荣
 * @date: 2023/7/13 11:14
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    // 注册
    @PostMapping("register")
    public ResponseResult register(User user){

        return userService.register(user);
    }

    // 登录
    @GetMapping("login")
    @LogOperation(description = Operation.OTHER,model = "用户模块")
    public ResponseResult login(LoginForm loginForm, HttpSession session){

        return userService.login(loginForm,session);
    }


    // 分页
    @GetMapping("selectUserPage")
    @LogOperation(description = Operation.SELECT,model = "用户模块")
    public ResponseResult getPage(UserPageDTO userPageDTO) {

        return userService.getPage(userPageDTO);
    }

    // 添加用户
    @PostMapping("add")
    @LogOperation(description = Operation.ADD,model = "用户模块")
    public ResponseResult add(@RequestBody User user){
        return userService.add(user);
    }

    @GetMapping("delete")
    @LogOperation(description = Operation.DELETE,model = "用户模块")
    public ResponseResult delete(Integer[] ids){
        return userService.batchDelete(ids);
    }

    @PostMapping("edit")
    @LogOperation(description = Operation.UPDATE,model = "用户模块")
    public ResponseResult edit(@RequestBody User user){
        return userService.edit(user);
    }

    @GetMapping("getByUsername")
    @LogOperation(description = Operation.OTHER,model = "用户模块")
    public User getByUsername(String username){
        return userService.getByUsername(username);
    }

    @GetMapping("logout")
    //@LogOperation(description = Operation.OTHER,model = "用户模块")
    public ResponseResult logout(HttpSession session){
        //  销毁session,配合拦截器，使退出后无法再直接通过网页来访问主页面
        session.invalidate();
        return ResponseResult.success(CodeMsg.SUCCESS);
    }

    @GetMapping("changePwd")
    @LogOperation(description = Operation.OTHER,model = "用户模块")
    public ResponseResult changePwd(String oldPwd,String newPassword,String username){
        return userService.changePwd(oldPwd,newPassword,username);


    }

}
